/*ParallelPlatform Project - A web platform to distribute and share knowledge
Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.*/

var link = [
"/media/js/ext-3.4.0/ext-jquery-adapter.js",
"/media/js/ext-3.4.0/ext-all.js",
'/media/aloha/aloha-nodeps.js',
'/media/aloha/plugins/com.gentics.aloha.plugins.Format/plugin.js',
//'/media/aloha/plugins/com.gentics.aloha.plugins.Table/plugin.js',
'/media/aloha/plugins/com.gentics.aloha.plugins.List/plugin.js',
'/media/aloha/plugins/com.gentics.aloha.plugins.Link/plugin.js',
'/media/aloha/plugins/com.gentics.aloha.plugins.Link/internalLink.js',
'/media/aloha/plugins/com.parallelplatform.aloha.plugins.Sections/plugin.js',
'/media/aloha/plugins/com.parallelplatform.aloha.plugins.Save/plugin.js',
'/media/aloha/plugins/com.parallelplatform.aloha.plugins.Resources/plugin.js',
'/media/aloha/plugins/com.parallelplatform.aloha.plugins.ImgLink/plugin.js',
'/alohaSettings/']

var textCss = ''/*'<link href="/media/css/dialog.css" type="text/css" rel="stylesheet" />'*/;
	//'<link href="/media/aloha/plugins/com.gentics.aloha.plugins.Table/resources/table.css" type="text/css" rel="stylesheet" />'+
	//'<link href="/media/css/flash.css" type="text/css" rel="stylesheet" />'+

function loaderAloha(after){
	
	var loadingPanel = jQuery('<div class="loadingPanel">'+
							  '<p><span>Loading Online Text Editor</span><br>Please Wait</p>'+
							  '<div class="progresBar"><div></div></div></div>');
	loadingPanel.css('display','none');
	$("body").append(loadingPanel);
	loadingPanel.fadeIn('fast');
	
	$("head").append(jQuery(textCss));
	$("head").append(jQuery('<link href="/media/css/generic/pageEditing/style.css" type="text/css" rel="stylesheet"/>'));
	loadScript(0,after);
}

function loadScript(src,after){
	$.getScript(link[src], function(data, textStatus){
		//console.log("iteracion "+src)
		$(".progresBar > div").animate( {width: Math.min((src+1)*(100/link.length),100)+'%'} ,'fast');
		if (src == link.length-1){
			GENTICS.Aloha.init();
			//TODO: anadir animacion de resaltado del contenido editable. ese contenido esta en GENTICS.Aloha.editables[i].obj[0] ver como se itera en la funcion GENTICS.Aloha.Save.addSave de plugins Save de aloha
			$(".loadingPanel > p").html("<span>Loading finished!</span><br>You can now start editing");
			var ready = $(".loadingPanel > p");
			$(".loadingPanel").delay(3000).fadeOut("slow");
			
			// Make the button to trigger the next function  
			after();
			// Now add a button to save at the end of the Article
			var saveButton = jQuery('<div class="extraSaveButton"><button class="extraSaveButton">'+ GENTICS.Aloha.Save.i18n("button.labelSave") +'</button></div>');
			saveButton.find("button").click(function(){
				GENTICS.Aloha.Save.save(); // Call save function of Aloha editor
			});
			$("article").append(saveButton);
			$("article").prepend(saveButton.clone(true));

		}else{
			loadScript(src+1,after);
		}
	});
}

